package com.yaoli.niuke;

import com.yaoli.ajob.ListNode;

/**
 * Created by siemens on 5/9/2017.
 */
public class _007_Test {
    public static void main(String[] args) {
        _007_Test test = new _007_Test();

        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(3);
        ListNode node66 = new ListNode(4);
        ListNode node3 = new ListNode(7);

        node1.next = node2;
        node2.next = node66;
        node66.next = node3;

        ListNode node5 = new ListNode(2);
        ListNode node6 = new ListNode(4);
        ListNode node7 = new ListNode(6);

        node5.next = node6;
        node6.next = node7;
        test.Merge(node1,node5);
    }

    //设立一个临时节点

    //如果想记住第一个节点，使用一个临时变量，

    //然后临时变量 .next赋值
    public ListNode Merge(ListNode list1,ListNode list2) {

        ListNode point = new ListNode(-1);
        ListNode res = point;

        while(list1 != null && list2!= null) {

            if(list1.val <= list2.val){
                point.next = list1;
                list1 = list1.next;

            }else if(list1.val > list2.val) {
                point.next = list2;
                list2 = list2.next;
            }

            point = point.next;

        }

//        while(list1 != null){
//            point.next = list1;
//            point = point.next;
//            list1 = list1.next;
//        }
//
//        while(list2 != null){
//            point.next = list2;
//            point = point.next;
//            list2 = list2.next;
//        }
        if(list1 != null){
            point.next = list1;
        }

        if(list2 != null){
            point.next = list2;
        }

        return res.next;
    }
}
